<?php

namespace App\Admin\Controllers;

use App\Models\Cabinet;
use App\Models\CabinetLiveText;
use App\Models\CabinetPoint;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
// use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use App\Models\ProjectUser;
use App\Models\Project;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Admin\Actions\Batch\CopyCabinetLiveText;
use App\Admin\Actions\Batch\CopyCabinetLiveTextAcrossBox;
use App\Admin\Actions\Grid\LiveTextUpload;

class CabinetLiveTextController extends Controller
{
    public function index(Content $content, $boxId = 0)
    {
        Admin::script(
            <<<JS
    var table = document.getElementById('grid-table');
    table.style.cssText="background-color:#e3e5eb;"
JS            
        );
        return $content
            ->body(headmenu($boxId,6))
            ->description('')
            ->body($this->grid($boxId));
    }

    protected function grid($boxId)
    {
        $optionBoxes = Cabinet::whereIn('id',[$boxId])->pluck('name', 'id')->toArray();
        $optionCabinets = Cabinet::where('pid',$boxId)->where('show', Cabinet::SHOW_YES)->pluck('name', 'id')->toArray();
        $cabinets = Cabinet::where('pid',$boxId)->pluck('id')->toArray();
        $optionPoints = CabinetPoint::whereIn('cabinet_id',$cabinets)->pluck('name', 'id')->toArray();
        $m = CabinetLiveText::where('cabinet_pid', $boxId);
        return new Grid($m, function (Grid $grid) use ($optionBoxes,$optionCabinets, $optionPoints, $boxId) {
            $grid->column('cabinet_pid', '云盒')->using($optionBoxes);
            $grid->column('cabinet_id', '设备')->using($optionCabinets);
            $grid->column('point_id', '采集点')->using($optionPoints);
            $grid->column('name','文本名称');
            $grid->column('condition', '条件')->using(CabinetLiveText::CONDITION_TEXT);
            $grid->column('value','匹配值');

            $grid->batchActions(function ($batch) {
                $batch->add(new CopyCabinetLiveText('<i class="fa fa-copy"></i>&nbsp;复制'));
                $batch->add(new CopyCabinetLiveTextAcrossBox());
            });

            $grid->filter(function (Grid\Filter $filter) use ($optionCabinets) {
                $filter->panel();
                $filter->equal('cabinet_id', '设备')->select($optionCabinets)->load('point_id', '/api/point_id');
                $filter->equal('point_id', '采集点')->select()->load('alarm_id', '/api/alarm_id');
            });

            $grid->tools(LiveTextUpload::make()->setKey($boxId));
        });
    }

    public function update($boxId, $id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content, $boxId){
        $box = Cabinet::find($boxId);
        return $content
            ->title('新建实况文本')
            ->description($box['name'])
            ->body($this->form($boxId));
    }

    public function form($boxId = 0){
        $box = Cabinet::find($boxId);
        return Form::make(new CabinetLiveText(), function (Form $form) use ($box) {
            $cabinets = $box ? Cabinet::where('pid', $box['id'])->pluck('name', 'id')->toArray() : [];
            $form->select('cabinet_id', '柜子')->options($cabinets)->load('point_id', '/api/point_id')->required();
            $form->text('name', '文本名称')->required();
            $form->select('point_id', '采集点')->required();
            $form->select('condition', '条件')->options(CabinetLiveText::CONDITION_TEXT)->required();
            $form->text('value', '匹配值')->required();
            $cabinetPid = $box ? $box['id'] : 0;
            $projectId = $box ? $box['project_id'] : 0;
            $form->hidden('cabinet_pid')->value($cabinetPid);
            $form->hidden('project_id')->value($projectId);
            $form->hidden('point_alias');
        
            $form->saving(function (Form $form) {
                $point = CabinetPoint::find($form->point_id);
                $form->point_alias = $point['alias'];

                if (!$form->isCreating()) {
                    $delData = [
                        'point_alias' => $form->model()->point_alias,
                        'name' => $form->model()->name,
                        'condition' => $form->model()->condition,
                        'value' => $form->model()->value,
                    ];
                    CabinetLiveText::delTextCache($delData);
                }
            });

            $form->saved(function (Form $form) {
                $point = CabinetPoint::find($form->point_id);
                $textData = [
                    'name' => $form->name,
                    'cond' => $form->condition,
                    'val' => $form->value,
                ];
                CabinetLiveText::setTextCache($point['alias'], $textData);
            });
            $form->deleting(function (Form $form) {
                $data = $form->model()->toArray();
                CabinetLiveText::delTextCache(reset($data));
            });
        });
    }

    public function edit($boxId, $id, Content $content){
        $box = Cabinet::find($boxId);
        return $content
            ->title('实况文本编辑')
            ->description($box['name'])
            ->body($this->form($boxId)->edit($id));
    }

    public function destroy($boxId, $id)
    {
        return $this->form()->destroy($id);
    }

    public function show($boxId, $id, Content $content){
        $one = CabinetLiveText::find($id);
        $box = Cabinet::find($boxId);
        $cabint = Cabinet::find($one['cabinet_id']);
        return $content
            ->translation($cabint['name'])
            ->title($cabint['name'])
            ->description($box['name'])
            ->body($this->detail($id));
    }

    public function detail($id){
        return Show::make($id, new CabinetLiveText(), function (Show $show) {
            $show->field('name','名称');
            $show->field('condition','条件')->using(CabinetLiveText::CONDITION_TEXT);
            $show->field('value','匹配值');
        });
    }
}